package com.lichenggod.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

@RestController
@Slf4j
public class HelloController {
    private String port;
    private String appName;

    public HelloController(DiscoveryClient discoveryClient) {
        this.discoveryClient = discoveryClient;
    }

    @Value("${server.port}")
    public void setPort(String port) {
        this.port = port;
    }

    @Value("${spring.application.name}")
    public void setAppName(String appName) {
        this.appName = appName;
    }

    private DiscoveryClient discoveryClient;


    @GetMapping("/hello")
    public  String helloTest(HttpServletRequest request){
        System.out.println( appName+"@"+port+"被调用!");
        List<String> instances = discoveryClient.getServices();
        if(instances.get(0)!=null){
            log.info("{}@{}:{} 被访问",appName,instances.get(0),port);
        }
        log.info("{} :{} 被访问",appName, port);
        request.getParameterMap().forEach((k,v)-> System.out.println("param "+k+" is :"+ Arrays.toString(v)));
        return "hello,我来自服务-hello，name："+appName+", port: "+port;
    }
}
